<template>
<el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="50%">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <el-row>
            <el-col :span="12" class="my-col col-left">
                <el-form-item label="名称" prop="title">
                    <el-input v-model="ruleForm.title" placeholder="请填写Picker名称"></el-input>
                </el-form-item>
                <el-form-item label="字段" prop="field">
                    <el-input v-model="ruleForm.field" placeholder="字段如store"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="ruleForm.sort"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12" class="my-col">
                <el-form-item label="规则" prop="sort">
                    <el-button type="primary" size="mini" @click="appendRule">添加</el-button>
                    <div class="rows">
                        <template v-for="(item,index) in ruleForm.value">
                            <el-row :key="index" class="table">
                                <el-col :span="8" class="table-cell">
                                    <el-input v-model="item.id" placeholder=""></el-input>
                                </el-col>
                                <el-col :span="8" class="table-cell">
                                    <el-input v-model="item.title" placeholder=""></el-input>
                                </el-col>
                                <el-col :span="8" class="table-cell">
                                    <el-button type="danger" size="mini" @click="removeRule(index)">删除</el-button>
                                </el-col>
                            </el-row>
                        </template>
                    </div>
                </el-form-item>
            </el-col>
        </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import {
    getFormToken
} from "@/api/system/system";
import request from '@/utils/request';

const ruleForm = {
    id: "",
    title: "",
    field: "",
    sort: 0,
    value: [],
    form_token: "",
};

const pickerItem = {
    id: '',
    title: '',
}

export default {
    data() {
        return {
            dialog: {
                title: "",
                visible: false,
                type: "",
            },
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                title: [{
                        required: true,
                        message: "渠道名称必填",
                        trigger: "blur",
                    },
                    {
                        min: 2,
                        max: 30,
                        message: "长度在 2 到 30 个字符",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    methods: {
        showAddView() {
            let dialog = {
                title: "添加Picker",
                visible: true,
                type: "create",
            };
            this.dialog = dialog;
            // 初始化表单
            this.ruleForm = Object.assign({}, ruleForm);
            // 获取表单的token
            getFormToken({
                api: "/api/system.dictionary/create",
            }).then(res => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        showSaveView(row) {
            let dialog = {
                title: "编辑Picker",
                visible: true,
                type: "save",
            };
            this.dialog = dialog;
            // 初始化表单
            this.ruleForm = Object.assign({}, row);
            // 获取表单的token
            getFormToken({
                api: "/api/system.dictionary/save",
            }).then(res => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        appendRule() {
            let row = Object.assign({}, pickerItem);
            this.ruleForm.value.push(row);
        },
        removeRule(index) {
            this.ruleForm.value.splice(index,1);
        },
        submit() {
            this.$refs["ruleForm"].validate((valid) => {
                // 验证失败阻止提交
                if (!valid) return false;
                // 提交表单
                if (this.dialog.type == "create") {
                    request({
                            url: "/api/system.dictionary/create",
                            method: "post",
                            data: this.ruleForm
                        }).then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("getTableData", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
                } else {

                    request({
                            url: "/api/system.dictionary/save",
                            method: "post",
                            data: this.ruleForm
                        })
                        .then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("getTableData", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
                }
            });
        },
    },
};
</script>

<style lang="less" scoped>
.my-col {
    padding: 0 20px;
}

.col-left {
    border-right: 1px solid #eee;
}

.rows {
    margin-top: 20px;
    .table {
        .table-cell {
            padding: 0 10px 10px 0;
        }
    }
}
</style>
